"use strict";
var utils = require('../../utils/utils.js');
var storage = require('../../utils/storage.js')
var request = require('../../utils/request.js')
var auth = require('../../utils/auth.js');
var config = require('../../config.js');
Page({
    /**
     * 页面的初始数据
     */
    data: {
        animationProgress1: '',
        animationProgress: '',
        userIndex: 0,
        userInfo: '',
        curData: {
            avatar: '',
            user_name: '',
            company: '',
            id: ''
        },
        account: [],//用户列表
        curIndex: 0,
        isInit: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var userInfo = storage.get("user_info")
        this.setData({
            userInfo: userInfo
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let that = this;
        var animation = wx.createAnimation({
            duration: 500,
            transformOrigin: "50% 50%",
            timingFunction: 'ease',
        })
        that.animation = animation;


        var animation1 = wx.createAnimation({
            duration: 500,
            transformOrigin: "50% 50%",
            timingFunction: 'ease',
        })
        that.animation1 = animation1;


        var next = function () {
            var avatar = wx.getStorageSync('user_info')['head_img'];
            that.setData({
                'curData.avatar': avatar,
                hidePage: false
            })
            that.getAccountInfo();
            // console.log(avatar);
            // debugger;
            // wx.setStorageSync('id',avatar);
        }
        auth(next);
    },
    bindPickerChange(e) {
        let that = this;
        // console.log('picker发送选择改变，携带值为', e.detail.value)
        that.switchAccountTap(e);
    },

    // // 设置动画
    // startProgressAnimation(isProgressShow) {
    //     var that = this
    //     that.setData({
    //         isInit: true,
    //     });
    //     setTimeout(function () {
    //         isInit: false
    //     }, 1000);
    //
    //
    //
    //     if (isProgressShow) {
    //         that.animation.bottom(0).step()
    //     } else {
    //         that.animation.bottom(-100 + '%').step()
    //     }
    //     that.setData({
    //         animationProgress: that.animation.export()
    //     })
    //
    //     console.log(isProgressShow)
    //     if (isProgressShow) {
    //         that.setData({
    //             'isShowAnimation1': 0
    //         });
    //
    //         setTimeout(function () {
    //             that.animation1.opacity(1).step()
    //             that.setData({
    //                 animationProgress1: that.animation1.export()
    //             })
    //         }, 0);
    //
    //     } else {
    //         that.animation1.opacity(0).step()
    //         that.setData({
    //             animationProgress1: that.animation1.export()
    //         })
    //         setTimeout(function () {
    //             that.setData({
    //                 'isShowAnimation1': 2000
    //             });
    //         }, 500);
    //     }
    //
    //
    // },
    // hideProgressTap: function (e) {
    //     let that = this;
    //     if(!this.data.isInit){
    //         return false;
    //     }
    //     this.startProgressAnimation(false);
    // },
    // // 打开选择管理员
    // changeAccountTap() {
    //     let that = this;
    //     if(!this.data.isInit){
    //         return false;
    //     }
    //     this.showProgressTap(true);
    //
    // },
    // showProgressTap(e) {
    //     this.startProgressAnimation(true);
    // },
    getAccountInfo() {
        var that = this;
        var mobile = wx.getStorageSync('mobile');
        request.get({'path': config.getList}, {mobile: mobile}).then(function (res) {
            var ratingData = res.data;

            if (ratingData.ret == 0 && ratingData.data.length > 0) {
                let nowUserId = wx.getStorageSync('id');
                let trueId = 0;
                wx.setStorageSync('ratingData', ratingData.data);
                if (nowUserId) {
                    ratingData.data.forEach(function (val, index) {
                        console.log(index, val)
                        if (val.id == nowUserId) {
                            trueId = index;
                        }
                    })
                }

                var curData = ratingData.data[trueId];
                that.setData({
                    isInit: true,
                    'account': ratingData.data,
                    'curData.user_name': curData['user_name'],
                    'curData.company': curData['company'],
                    'curData.id': curData['id']
                });
                wx.setStorageSync('id', curData['id']);
                // that.hideProgressTap();
            } else {
                wx.showToast({
                    icon: 'none',
                    title: ratingData.msg
                })
                return false;
            }
        }, function (res) {
            wx.showToast({
                icon: 'none',
                title: res.data.msg
            })
            return false;
        })
    },
    // // 选择管理员
    switchAccountTap(e) {
        let that = this;
        let account = that.data.account;
        var index = e.detail.value;
        var selectObject = account[index];
        var id = selectObject.id;
        var username = selectObject.user_name;
        var company = selectObject.company;

        request.post({'path': config.change_user}, {id: id}).then(function (res) {
            var ratingData = res.data;
            if (ratingData.ret) {
                wx.showToast({
                    icon: 'none',
                    title: res.data.msg
                })
                return false;
            }
            that.setData({
                'curIndex': index,
                'curData.user_name': username,
                'curData.id': id,
                'curData.company': company
            });
            wx.setStorageSync('id', id);

        }, function (res) {
            wx.showToast({
                icon: 'none',
                title: res.data.msg
            })
            return false;
        })


        // this.hideProgressTap();
    },
    logOut() {
        //退出登录
        request.post({'path': config.quit_logi}, {}).then(function (res) {
            var ratingData = res.data;
            debugger;
            wx.removeStorageSync('mobile');
            wx.removeStorageSync('id');
            wx.removeStorageSync('wxa_session_id');
            wx.removeStorageSync('user_info');
            wx.removeStorageSync('wxa_time_stamp');
            wx.removeStorageSync('ratingData');

            wx.navigateTo({
                url: '/pages/login/login'
            })

        }, function (res) {
            wx.showToast({
                icon: 'none',
                title: res.data.msg
            })
            return false;
        })


    }
})